package com.heima.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.UserSearchDto;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.feign.BehaviorFeign;
import com.heima.search.mapper.ApUserSearchMapper;
import com.heima.search.service.ApUserSearchService;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP用户搜索信息表 服务类
 * </p>
 *
 * @author itheima
 */
@Service
@Log4j2
public class ApUserSearchServiceImpl extends ServiceImpl<ApUserSearchMapper, ApUserSearch> implements ApUserSearchService {

    @Autowired
    private BehaviorFeign behaviorFeign;


    /**
     * 查询搜索历史
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findUserSearch(UserSearchDto dto) {
        //校验参数
        if (dto.getPageSize() > 50) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.查询行为实体
        ApBehaviorEntry apBehaviorEntry = getEntry(dto);
        if (apBehaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        IPage<ApUserSearch> page = new Page<>(0, dto.getPageSize());
        LambdaQueryWrapper<ApUserSearch> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUserSearch::getEntryId, apBehaviorEntry.getId())
                .eq(ApUserSearch::getStatus, 1);
        IPage<ApUserSearch> iPage = page(page, wrapper);
        return ResponseResult.okResult(page.getRecords());
    }

    //行为实体
    private ApBehaviorEntry getEntry(UserSearchDto dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        ApBehaviorEntry apBehaviorEntry = behaviorFeign.findByUserIdOrEntryId(user.getId(), dto.getEquipmentId());
        return apBehaviorEntry;
    }

    /**
     * 删除搜索历史
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult delUserSearch(UserSearchDto dto) {
        //1.检查参数
        if (dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.跟新搜索状态为0
        ApBehaviorEntry apBehaviorEntry = getEntry(dto);
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaUpdateWrapper<ApUserSearch> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(ApUserSearch::getEntryId,apBehaviorEntry.getId())
                .eq(ApUserSearch::getId,dto.getId())
                .set(ApUserSearch::getStatus,0);
        update(wrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 插入搜索记录
     *
     * @param
     * @return
     */
    @Override
    public void insert(Integer entryId, String searchWords) {
        //1.查询当前搜索记录
        LambdaQueryWrapper<ApUserSearch> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUserSearch::getEntryId, entryId);
        wrapper.eq(ApUserSearch::getKeyword, searchWords);
        ApUserSearch userSearch = getOne(wrapper);
        if (userSearch == null) {
            ApUserSearch apUserSearch = new ApUserSearch();
            //保存搜索记录
            apUserSearch.setCreatedTime(new Date());
            apUserSearch.setEntryId(entryId);
            apUserSearch.setKeyword(searchWords);
            apUserSearch.setStatus(1);
            save(apUserSearch);
        } else if (userSearch != null && userSearch.getStatus() == 0) {
            //更新搜索记录
            userSearch.setStatus(1);
            updateById(userSearch);
        } else if (userSearch != null && userSearch.getStatus() == 1) {
            log.info("关键字已存在,无需保存!!");
            return;
        }
    }
}
